 <template>
  <div>
    <div class="top">
      <span class="fh"><van-icon name="arrow-left" @click="fh" /></span>
      <span class="zc">注册</span>
    </div>
    <div class="mainCenter">
      <div class="picture">
        <van-icon name="qr-invalid" class="tp" />
        <br />
        <span>我们将以短信的形式将验证码发送给您</span>
      </div>
      <div>
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            placeholder="请输入短信验证码"
            :rules="[{ required: false, message: '请输入短信验证码' }]"
            class="srk"
            @input="yanzheng"
          />
          <van-button round type="info" class="fs" @click="send"
            >发送验证码</van-button
          >
          <div style="margin: 16px">
            <van-button
              round
              block
              type="info"
              native-type="submit"
              @click="zc"
              :disabled="disabled_flag"
              >下一步</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { Toast } from "vant";

export default {
  name: "PhoneTest",
  data() {
    return {
      username: "",
      userphone: "",
      telcodes: "",
      disabled_flag: true,
    };
  },
  methods: {
    fh() {
      this.$router.go(-1);
    },
    onSubmit(values) {
      console.log("submit", values);
    },
    yanzheng() {
      let reg = new RegExp(/^\d{5,}$/);
      if (reg.test(this.username)) {
        this.disabled_flag = false;
      }
    },
    send() {
      let tel = this.userphone;
      console.log("tel: ", tel);
      this.$service({
        url: "api/user/dosendmsgcode",
        data: {
          tel,
        },
        method:"post"
      })
        .then((res) => {
          console.log(res, "res");
          console.log("验证码：", res.data.data);
          // this.telcodes=res.data.data
          // console.log('telcodes: ', this.telcodes);
        })
        .catch((err) => {
          console.log(err, "err");
        });
    },
    zc() {
      let tel = this.userphone;
      console.log("tel: ", tel);
      this.$service
        .post("/api/user/docheckcode", {
          tel,
          telcode: this.username,
        })
        .then((res) => {
          console.log(res, "res");
          if (res.data.code == "10007") {
            Toast("验证码错误");
            return;
          }
          if (res.data.code == "200") {
            this.$router.push({ path: "/PhoneRegisterPsd" });
          }
        });
    },
  },
  created() {
    this.userphone = JSON.parse(localStorage.getItem("userphone"));
    console.log(this.userphone);
    // console.log(111);
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}
.top {
  height: 3rem;
  text-align: center;
  line-height: 3rem;
  border-bottom: 2px solid gray;
  position: relative;
}
.fh {
  position: absolute;
  font-size: 20px;
  left: 1rem;
}
.yhm {
  height: 4rem;
  line-height: 4rem;
  margin-bottom: 1rem;
}
.dl {
  position: absolute;
  right: 0.5rem;
}
.picture {
  text-align: center;
  font-size: 12px;
  margin-top: 4rem;
  margin-bottom: 2rem;
}
.tp {
  margin-bottom: 2rem;
  font-size: 2.5rem;
}
.fs {
  position: absolute;
  right: 0;
  top: 13.9rem;
  font-size: 10px;
  width: 6rem;
}
.srk {
  width: 16rem;
  margin-left: 0.7rem;
}
</style>